var register = {
    isPhotoNum: false,
    md5Chone: null,
    timestamp: Date.parse(new Date()),
    smsClicked: 1,
    timer: null,
    imgPic: 1,
    sec: 60,
    GetQueryString: function (name){
        var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if(r!=null)return  unescape(r[2]); return null;
    },
    reset: function() {
        var _this = register;
        clearInterval(_this.timer);
        $(".verify").html("获取验证码");
        $(".verify").removeClass("gray");
        _this.sec = 60;
        _this.smsClicked = 1
    },
    num: function() {
        var _this = register;
        _this.sec--;
        if (_this.sec < 10) {
            _this.sec = "0" + _this.sec
        }
        $(".verify").html(_this.sec + "s后重发");
        if (_this.sec == 0) {
            _this.reset()
        }
    },
    verifyClick: function(verify) {
        var _this = register;
        var phoneReg = /1[3,4,5,7,8,9]\d{9}/;
        var mobile = $(".phone").val();
        var imgCode = $(".imgCode").val();
        if (mobile === "") {
            _this.showAlert("请输入手机号");
            $(".phone").val("");
            return
        }
        if (!phoneReg.test(mobile)) {
            _this.showAlert("请输入正确的手机号");
            $(".phone").val("");
            return
        }
        if (!_this.smsClicked) {
            return
        }
        if (_this.isPhotoNum) {
            if (imgCode == null) {
                _this.showAlert("请输入图形验证码");
                return
            }
        }
        _this.smsClicked = 0;
        $(".verify").text("60s后重发");
        $(".verify").addClass("gray");
        _this.timer = setInterval(_this.num, 1000);
        _this.getSeccode(mobile, imgCode)
    },
    getSeccode: function(mobile, imgCode) {
        var _this = register;
        $.ajax({
            type: "get",
            url: rootUrl + "/gateway/send",
            data: {
                tele: mobile,
                code: imgCode,
            },
            headers: {
                "X-App-Id": "mhUBL76CW1Fxvoe0",
                "X-Timestamp": _this.timestamp,
                'X-Product-Id': 'PT4xz9uKP3TiTMmk',
                'X-Platform': 'H5',
                'X-Referer': 'register-page',
                'channel': $("#item").val()
            },
            success: function(data) {
                if (data.code === 200) {
                } else {
                    if (data.code === 399) {
                        _this.reset();
                        _this.showImgCode();
                        _this.imgPic = 0;
                        _this.showAlert("请先输入图形验证码");
                        _this.isPhotoNum = true
                    } else if (data.code === 400) {
                        _this.reset();
                        _this.showAlert("验证码失效，请重试");
                        $(".imgCode").val("");
                        _this.getImg();
                    } else if (data.code === 401) {
                        _this.reset();
                        _this.showAlert("验证码错误，请重试");
                        $(".imgCode").val("")
                    } else if(data.code === 402) {
                        _this.showAlert("手机号已注册");
                        window.location.href = data.message;
                    } else if(data.code === 403) {
                        _this.reset();
                        _this.showAlert("短信发送过于频繁");
                        $(".imgCode").val("")
                        _this.getImg();
                    } else {
                        _this.reset();
                        _this.showAlert("发送失败，请重新发送");
                    }
                }
            },
            error: function() {
                _this.reset();
                _this.showAlert("链接错误，刷新重试！")
            }
        })
    },
    getImg: function() {
        $(".img").attr('src',rootUrl + "/gateway/code/w/100/h/40?ts="+new Date().getTime());
    },
    showImgCode: function() {
        var _this = register;
        $(".img-yzm-bor").show();
        if (_this.imgPic === 1) {
            _this.getImg()
        }
    },
    login: function(mobile,imgCode, seccode) {

    },
    showAlert: function(msg) {
        $("body").append($("<div id='message' style='display:none'><p>" + msg + "</p></div>"));
        $("#message").css({
            "display": "block",
            "position": "fixed",
            "top": "35%",
            "left": "50%",
            "margin-left": "-82.5px",
            "padding": "10px 15px",
            "border-radius": "5px",
            "background-color": "rgba(0,0,0,0.65)",
            "width": "150px",
            "z-index": 10,
            "font-size": "14px",
            "color": "#fff",
            "text-align": "center",
            "z-index": "2000000"
        });
        setTimeout(function() {
                $("#message").css({
                    "display": "none"
                });
                $("#message").remove()
            },
            1500)
    },
    init: function(verify) {
        this.verifyClick(verify)
    },
};
$(".register-down").click(function () {
    var _this = register;
    var mobile = $(".phone").val();
    var imgCode = $(".imgCode").val();
    var seccode = $(".verifyCode").val();
    var channelId = $("#item").val();
    var phoneReg = /1[3,4,5,7,8]\d{9}/;
    if (mobile == "") {
        _this.showAlert("请输入手机号");
        return
    }
    if (!phoneReg.test(mobile)) {
        _this.showAlert("请输入正确的手机号");
        $(".phone").val("");
        return
    }
    if (_this.isPhotoNum) {
        if (imgCode == null || imgCode === "null") {
            _this.showAlert("请输入图形验证码");
            return
        }
    }
    if (seccode == "") {
        _this.showAlert("请填写验证码");
        return
    }
    // _this.md5Chone = sha256_digest("/api/v1/users/login", {
    //         "mobile": mobile,
    //         "validateCode": imgCode,
    //         "seccode": seccode
    //     },
    //     _this.timestamp, "GET");
    $.ajax({
        type: "post",
        url: rootUrl + "/gateway/reg",
        contentType:"application/json",
        data: JSON.stringify({
            phone: mobile,
            code: seccode,
            channelId: channelId,
            ip: ip,
        }),
        headers: {
            "X-App-Id": "mhUBL76CW1Fxvoe0",
            "X-Timestamp": _this.timestamp,
            'X-Product-Id': 'PT4xz9uKP3TiTMmk',
            'X-Platform': 'H5',
            'X-Referer': 'register-page'
        },
        success: function(data) {
            if (data.code === 400) {
                _this.showAlert(data.message);
            } else if(data.code === 501 || data.code === 502) {
                $(".verifyCode").val("");
                if (_this.isPhotoNum) {
                    _this.getImg();
                    $(".imgCode").val("")
                }
                _this.showAlert(data.message);
                _this.reset()
            } else {
                _this.reset();
                if (data.data) {
                    window.location.href = data.data;
                }
            }
        },
        error: function() {
            _this.showAlert("链接错误，刷新重试！")
        }
    })
});